/* ---------------------------- dom/document.js ----------------------------- */
VED.dom.document = VED.dom.domObject.extend({
   initialize: function( domDocument ) {
      this.parent( domDocument );
   },

   type: VED.NODE_DOCUMENT,

   appendStyleSheet: function( cssFileUrl ) {
      if ( this.$.createStyleSheet )
         this.$.createStyleSheet( cssFileUrl );
      else {
         var link = new VED.dom.element( 'link' );
         link.setAttributes({
            rel: 'stylesheet',
            type: 'text/css',
            href: cssFileUrl
         });

         this.getHead().append( link );
      }
   },

   appendStyleText: function( cssStyleText ) {
      if ( this.$.createStyleSheet ) {
         var styleSheet = this.$.createStyleSheet( "" );
         styleSheet.cssText = cssStyleText;
      } else {
         var style = new VED.dom.element( 'style', this );
         style.append( new VED.dom.text( cssStyleText, this ) );
         this.getHead().append( style );
      }

      return styleSheet || style.$.sheet;
   },

   createElement: function( name, attribsAndStyles ) {
      var element = new VED.dom.element( name, this );

      if ( attribsAndStyles ) {
         if ( attribsAndStyles.attributes )
            element.setAttributes( attribsAndStyles.attributes );

         if ( attribsAndStyles.styles )
            element.setStyles( attribsAndStyles.styles );
      }

      return element;
   },

   createText: function( text ) {
      return new VED.dom.text( text, this );
   },

   focus: function() {
      this.getWindow().focus();
   },

   getActive: function() {
      return new VED.dom.element( this.$.activeElement );
   },

   getById: function( elementId ) {
      var $ = this.$.getElementById( elementId );
      return $ ? new VED.dom.element( $ ) : null;
   },

   getByAddress: function( address, normalized ) {
      var $ = this.$.documentElement;

      for ( var i = 0; $ && i < address.length; i++ ) {
         var target = address[ i ];

         if ( !normalized ) {
            $ = $.childNodes[ target ];
            continue;
         }

         var currentIndex = -1;

         for ( var j = 0; j < $.childNodes.length; j++ ) {
            var candidate = $.childNodes[ j ];

            if ( normalized === true && candidate.nodeType == 3 && candidate.previousSibling && candidate.previousSibling.nodeType == 3 ) {
               continue;
            }

            currentIndex++;

            if ( currentIndex == target ) {
               $ = candidate;
               break;
            }
         }
      }

      return $ ? new VED.dom.node( $ ) : null;
   },

   getElementsByTag: function( tagName, namespace ) {
      if ( !( VED.env.ie && !( document.documentMode > 8 ) ) && namespace )
         tagName = namespace + ':' + tagName;
      return new VED.dom.nodeList( this.$.getElementsByTagName( tagName ) );
   },

   getHead: function() {
      var head = this.$.getElementsByTagName( 'head' )[ 0 ];
      if ( !head )
         head = this.getDocumentElement().append( new VED.dom.element( 'head' ), true );
      else
         head = new VED.dom.element( head );

      return head;
   },

   getBody: function() {
      return new VED.dom.element( this.$.body );
   },

   getDocumentElement: function() {
      return new VED.dom.element( this.$.documentElement );
   },

   getWindow: function() {
      return new VED.dom.window( this.$.parentWindow || this.$.defaultView );
   },

   write: function( html ) {
      // Don't leave any history log in IE. (#5657)
      this.$.open( 'text/html', 'replace' );

      // Support for custom document.domain in IE.
      //
      // The script must be appended because if placed before the
      // doctype, IE will go into quirks mode and mess with
      // the editable, e.g. by changing its default height.
      if ( VED.env.ie )
         html = html.replace( /(?:^\s*<!DOCTYPE[^>]*?>)|^/i, '$&\n<script data-ved-temp="1">(' + VED.tools.fixDomain + ')();</script>' );

      this.$.write( html );
      this.$.close();
   }
});





